//
//  CustomerTableViewController.m
//  WebPDA
//
//  Created by Justin Yip on 7/28/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "AllPassengerViewController.h"
#import "PassengerCell.h"
#import "PassengerDetailViewController.h"
#import "Passenger.h"
#import "PassengerRepository.h"
#import "UIViewController+AutoAdjust.h"

@implementation AllPassengerViewController
@synthesize detailViewController;
@synthesize contentController;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	self.title = @"所有旅客";
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return [[[PassengerRepository sharedRepository] getRows] objectAtIndex:section];
}

- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView {
	return [[PassengerRepository sharedRepository] getRows];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return [[[PassengerRepository sharedRepository] getRows] count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	NSString *sectionStr = [[[PassengerRepository sharedRepository] getRows] objectAtIndex:section];
	return [[PassengerRepository sharedRepository] numberOfPassengerInRow:[sectionStr intValue]];
}

//
// 懒加载，根据indexPath.section 加载第几行的乘客，乘客仓储维护一份缓存，保存已加载的乘客
//
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	//NSLog(@"query cell section:%d, row:%d", indexPath.section, indexPath.row);
    
    static NSString *CellIdentifier = @"PassengerCell";
    
    PassengerCell *cell = (PassengerCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
		NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PassengerCell" owner:self options:nil];
		for (id oneObject in nib) {
			if ([oneObject isKindOfClass:[PassengerCell class]]) {
				cell = (PassengerCell*)oneObject;
			}
		}
    }
	
	Passenger *p = [[PassengerRepository sharedRepository] passengerInRow:indexPath.section atColumn:indexPath.row];
	cell.nameLabel.text = p.name;
	cell.seatLabel.text = p.seat;
	cell.descriptionLabel.text = [NSString stringWithFormat:@"%@%@,%@", p.level, @"W", p.cert];
	UIImage *image = [UIImage imageNamed:(indexPath.row % 2 == 0)?@"avatar1.png":@"avatar2.png"];
	if (image) {
		cell.imageView.image = image;
	}
	return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	
	Passenger *p = [[PassengerRepository sharedRepository] passengerInRow:indexPath.section atColumn:indexPath.row];
	
	if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
	{
		detailViewController = [[PassengerDetailViewController alloc] initWithSmartNib];
		[self.navigationController pushViewController:detailViewController animated:YES];
		[detailViewController setPassenger:p];
		[detailViewController release];
	}
	else
	{
		[contentController setPassenger:p];
	}
	//PassengerDetailViewController *detailViewController = [[PassengerDetailViewController alloc] initWithNibName:@"PassengerDetailViewController" bundle:nil];
	//[self.navigationController pushViewController:detailViewController animated:YES];
	//Passenger *p = [[PassengerRepository sharedRepository] passengerInRow:indexPath.section atColumn:indexPath.row];
	//detailViewController.passenger = p;
	
	//[detailViewController release];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[contentController release];
	//[detailViewController release];
    [super dealloc];
}


@end

